<?php
//过滤无效非法请求，待做


//加载phprpc
include_once 'Extend/phprpc/phprpc_server.php';
$rpc_server = new PHPRPC_Server();
$rpc_server->setDebugMode(true);
$rpc_server->setEnableGZIP(true);

/**
 * 获取订单
 * @param array $params
 * @return json string
 */
function orderGetServiceOrder($params = array()) {
	$params = explodeStringToArray($params);
	return makeRequest('http://'.$_SERVER['HTTP_HOST'].'/index.php/Order/getServiceOrders', $params);
}

/**
 * 导出订单
 * @param array $params
 * @return mixed
 */
function orderExportOrders($params=array()){
	$params = explodeStringToArray($params);
	return makeRequest('http://'.$_SERVER['HTTP_HOST'].'/index.php/Order/exportOrders', $params);
}

/**
 * 退款
 * @param array $params
 * @return json string
 */
function orderRefundUserOrder($params = array()) {
	$params = explodeStringToArray($params);
	return makeRequest('http://'.$_SERVER['HTTP_HOST'].'/index.php/Order/refundOrder', $params);
}

/**
 * 返回json编码结果
 * @param array $result
 * @return json string
 */
function getJsonEncodeResult($result) {
	return json_encode($result);
}

/**
 * 将string参数解析为array参数
 * @param string $params_str name/张三/sex/男
 * @return array
 */
function explodeStringToArray($params_str='') {
	$params_array = array();
	if(is_string($params_str)){
		$params_str = explode('/', $params_str);		
		foreach($params_str as $key => $each_param) {
			if ($key % 2 == 0) {
				$params_array [$each_param] = $params_str [$key + 1];
			} else {
				continue;
			}
		}
	}
	return $params_array;
}

/**
 * 刷新分配的订单
 * @param array $params
 * @return json string
 */
function orderFreshSignOrder($params = array()) {
	$params = explodeStringToArray($params);
	return makeRequest('http://www.baidu.com');
	return makeRequest('http://'.$_SERVER['HTTP_HOST'].'/index.php/Order/test', $params);
}

/**
 * curl请求
 * @param string $url
 * @param array $params 字段名为键值，字段数据为值的数组
 * @return mixed or boolean
 */
function makeRequest($url, $param) {
	$oCurl = curl_init();
	if (stripos($url, "https://") !== FALSE) {
		curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
	}
	curl_setopt($oCurl, CURLOPT_URL, $url);
	curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($oCurl, CURLOPT_POST, 1);
	curl_setopt($oCurl, CURLOPT_POSTFIELDS, $param);
	$sContent = curl_exec($oCurl);
	$aStatus = curl_getinfo($oCurl);
	curl_close($oCurl);
	if (intval($aStatus ["http_code"]) == 200) {
		return $sContent;
	} else {
		return FALSE;
	}
}

function test() {
	return 'test ok';
}

$rpc_server->add('orderGetServiceOrder');
$rpc_server->add('orderRefundUserOrder');
$rpc_server->add('orderFreshSignOrder');
$rpc_server->add('orderExportOrders');
$rpc_server->add('test');
$rpc_server->start();
?>